<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Device\Repository;

use App\Repository\IRepository;
use Hyperf\Database\Model\Builder;
use Plugin\Device\Model\RdDeviceSerial;

/**
 * 录像机设备 Repository类
 */
class RdDeviceSerialRepository extends IRepository
{
   public function __construct(
        protected readonly RdDeviceSerial $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 装置ID
        if (isset($params['rod_id']) && filled($params['rod_id'])) {
            $query->where('rod_id', '=', $params['rod_id']);
        }

        // 1上线 2下线
        if (isset($params['online']) && filled($params['online'])) {
            $query->where('online', '=', $params['online']);
        }

        // 名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 名称语音ID
        if (isset($params['voice_id']) && filled($params['voice_id'])) {
            $query->where('voice_id', '=', $params['voice_id']);
        }

        // 遵守语音ID
        if (isset($params['comply_id']) && filled($params['comply_id'])) {
            $query->where('comply_id', '=', $params['comply_id']);
        }

        // 流媒体地址
        if (isset($params['url']) && filled($params['url'])) {
            $query->where('url', '=', $params['url']);
        }

        // 设备序列号
        if (isset($params['device_serial']) && filled($params['device_serial'])) {
            $query->where('device_serial', 'like', '%'.$params['device_serial'].'%');
        }

        // 通道号
        if (isset($params['channel_no']) && filled($params['channel_no'])) {
            $query->where('channel_no', 'like', '%'.$params['channel_no'].'%');
        }

        // 设备类型 1:枪机 2:球机
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 站点id
        if (isset($params['stand_id']) && filled($params['stand_id'])) {
            $query->where('stand_id', '=', $params['stand_id']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 备注
        if (isset($params['remarks']) && filled($params['remarks'])) {
            $query->where('remarks', 'like', '%'.$params['remarks'].'%');
        }

        // 提示语ID
        if (isset($params['prompt_id']) && filled($params['prompt_id'])) {
            $query->where('prompt_id', '=', $params['prompt_id']);
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && filled($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        return $query;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with(['rod:id,name', 'stand:id,name', 'comply:id,file', 'voice:id,file'])
            ->paginate(
            perPage: $pageSize,
            pageName: static::PER_PAGE_PARAM_NAME,
            page: $page,
        );
        return $this->handlePage($result);
    }
}